package controllers;

import java.awt.event.ActionEvent;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Vector;

import dialogs.DeleteTupleDialog;
import dialogs.InsertTupleDialog;

import models.PurchaseModel;

import system.View;

public class PurchaseController extends AbstractMenuItemController {
	private PurchaseModel purchase = new PurchaseModel();

	public PurchaseController(View v) {
		super(v);
		abstractModel = purchase;
		purchase.addExceptionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();

		if (actionCommand.equals("Delete Purchase"))
			showDialogWindow(new DeleteTupleDialog(view, actionCommand, "Key",
					new String[] { "Receipt ID Number: " }, this));
		else if (actionCommand.equals("Insert Purchase"))
			showDialogWindow(new InsertTupleDialog(view, actionCommand,
					"Purchase", new String[] { "Customer ID: ", 
							"Store Name: ",
							"Credit Card Number: ",
							"Credit Card Expiry Date: ",
							"Expected Arrival Date:", "Date of Delivery: " },
					this));
	}

	@Override
	public int validateInsert(Vector<String> s) {
		if (s.size() != 6)
			return VALIDATIONERROR;

		try {
			String cid;
			String sname;
			String card_no;
			Date expire_date;
			Date expectedDate;
			Date deliveredDate;

			SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yy");

			if (s.get(0).length() != 0)
				cid = s.get(0).trim();
			else cid = null;
			if (s.get(1).length() != 0)
				sname = s.get(1).trim();
			else return VALIDATIONERROR;
			if (s.get(2).length() != 0)
				card_no = s.get(2).trim();
			else card_no = null;
			fm.applyPattern("MM/yy");
			if (s.get(3).length() != 0)
				expire_date = new Date(fm.parse(s.get(3).trim()).getTime());
			else expire_date = null;
			fm.applyPattern("dd/MM/yy");
			if (s.get(4).length() != 0)
				expectedDate = new Date(fm.parse(s.get(4).trim()).getTime());
			else expectedDate = null;
			if (s.get(5).length() != 0)
				deliveredDate = new Date(fm.parse(s.get(5).trim()).getTime());
			else deliveredDate = null;

			view.updateStatusBar("Inserting Purchase...");

			if (purchase.insert(cid, sname, card_no, expire_date, expectedDate, deliveredDate))
				return operationSuccess();
			else return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
		catch (ParseException ex){
			return VALIDATIONERROR;
		}

	}

	@Override
	public int validateDelete(Vector<String> s) {
		if (s.size() != 1)
			return VALIDATIONERROR;

		try {
			Integer receiptId;

			if (s.get(0).trim().length() != 0)
				receiptId = Integer.valueOf(s.get(0).trim());
			else return VALIDATIONERROR;

			view.updateStatusBar("Deleting Purchase...");

			if (purchase.delete(receiptId)) return operationSuccess();
			else return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
	}
}